﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/27 9:11:13
* 描述：Class1
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.IO;

namespace CommonLibrary
{
    /// <summary>
    /// 程序基础设置
    /// </summary>
    public class AppBasicSetting
    {
        /// <summary>
        /// 配置文件路径
        /// </summary>
        private static string CONFIG_FILE_PATH = Path.Combine(CommonHelper.COMMON_CONFIG_DIR, "App.json");
        /// <summary>
        /// 设备编码
        /// </summary>
        [Description("设备编码")]
        [JsonProperty(PropertyName = "EquipmentCode")]
        public string EquipmentCode { get; set; } = string.Empty;
        /// <summary>
        /// 工厂代码
        /// </summary>
        [Description("工厂代码")]
        [JsonProperty(PropertyName = "SiteCode")]
        public string SiteCode { get; set; } = "EVE01";
        /// <summary>
        /// 产线代码
        /// </summary>
        [Description("产线代码")]
        [JsonProperty(PropertyName = "LineCode")]
        public string LineCode { get; set; } = "L5";
        /// <summary>
        /// 软件名称
        /// </summary>
        [Description("软件名称")]
        [JsonProperty(PropertyName = "SoftwareName")]
        public string SoftwareName { get; set; } = string.Empty;

        /// <summary>
        /// 3153耐压测试仪触发测试单位，1-上位机，其他-外部I/O
        /// </summary>
        [Description("耐压测试仪触发测试单位")]
        [JsonProperty(PropertyName = "HIOKI3153ActiveUnit")]
        public int HIOKI3153ActiveUnit { get; set; } = 1;

        
        /// <summary>
        /// 锁
        /// </summary>
        private static object LOCK_OBJ = new object();
        /// <summary>
        /// 实例对象
        /// </summary>
        private static AppBasicSetting instance = null;
        /// <summary>
        /// 实例对象
        /// </summary>
        /// <param name="reload"></param>
        /// <returns></returns>
        public static AppBasicSetting Instance(bool reload = false)
        {
            if (reload || instance == null)
            {
                try
                {
                    instance = CommonHelper.GetJsonFromFile(CONFIG_FILE_PATH, new AppBasicSetting());
                }
                catch (Exception ex)
                {
                    instance = new AppBasicSetting();
                    LogHelper.Error(ex);
                }
            }
            return instance;
        }
        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            lock (LOCK_OBJ)
            {
                CommonHelper.SaveToJsonFile(instance, CONFIG_FILE_PATH);
                CommonHelper.SaveToJsonFile(instance, CONFIG_FILE_PATH);
            }
        }
        /// <summary>
        /// 描述信息
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return CommonHelper.GetObjectString(this);
        }
    }
}
